package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.exhandler.BusinessException;
import com.qf.pojo.R;
import com.qf.pojo.Student;
import com.qf.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public R findAll(){
        List<Student> students = studentService.findAll();

//        int i = 1/0;
        //对用户的年龄进行判断
//        int age = 150;
//        if (age > 105){
//            throw new BusinessException("年龄范围为0-105");
//        }

        return R.success(students);
    }

    @GetMapping("/{page}/{size}")
    public R page(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageInfo<Student> pageInfo = studentService.page(page,size);

        return R.success(pageInfo);
    }

    @PostMapping("/update")
    public R update(@RequestBody Student stu){

        studentService.update(stu);

        return R.success(null);
    }

    @PostMapping("/add")
    public R add(@RequestBody Student stu){

        studentService.add(stu);

        return R.success(null);
    }

    @PostMapping("/{number}")
    public R del(@PathVariable("number")String number){

        studentService.del(number);

        return R.success(null);
    }
}
